package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.atguigu.lease.model.enums.ItemType.APARTMENT;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
     ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    ApartmentFacilityService apartmentFacilityService;
    @Autowired
    ApartmentFacilityMapper apartmentFacilityMapper;
    @Autowired
        ApartmentLabelMapper  apartmentLabelMapper;
    @Autowired
    ApartmentFeeValueMapper apartmentFeeValueMapper;
    @Autowired
    GraphInfoMapper graphInfoMapper;
    @Autowired
    DistrictInfoService districtInfoService;
    @Autowired
    CityInfoService cityInfoService;
    @Autowired
    ProvinceInfoService provinceInfoService;
    @Autowired
    LabelInfoService labelInfoService;
    @Autowired
    ApartmentLabelService apartmentLabelService;
    @Autowired
    ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    FileService fileService;
    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    FeeValueService feeValueService;
    @Autowired
    FacilityInfoMapper facilityInfoMapper;
    @Autowired
    FeeValueMapper feeValueMapper;
    @Autowired
    LabelInfoMapper labelInfoMapper;



    @Override
    public IPage<ApartmentItemVo> pageItem(IPage<ApartmentItemVo> iPage, ApartmentQueryVo queryVo) {

        IPage<ApartmentItemVo>apartmentInfoVo=apartmentInfoMapper.pageItem(iPage,queryVo);

        return apartmentInfoVo ;
    }

    @Override
    public void removeApatmenById(Long id,  ItemType reason) {
        //判断是否还存在房间对象
        QueryWrapper<RoomInfo> roomInfoQueryWrapper = new QueryWrapper<>();
        roomInfoQueryWrapper.eq("apartment_id", id);
        Long count=roomInfoMapper.selectCount(roomInfoQueryWrapper);
Assert.isTrue(count<=0,"当前公寓下还有房间，不能删除");

        // 删除公寓信息表中的数据
        apartmentInfoMapper.deleteById(id);
        //删除公寓配套信息
        apartmentInfoMapper.deleteApartmentFacility(id);
        //删除公寓标签信息
        apartmentInfoMapper.deleteApartmentLabel(id);
        //删除公寓费用信息
        apartmentInfoMapper.deleteApartmentFeeValue(id);
        //删除图片
        apartmentInfoMapper.deleteApartmentImage(id,ItemType.APARTMENT.getCode());

    }

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {

//        Long id = apartmentSubmitVo.getId();


        ProvinceInfo provinceInfo =provinceInfoService.getById(apartmentSubmitVo.getProvinceId());
        CityInfo city = cityInfoService.getById(apartmentSubmitVo.getCityId());
        DistrictInfo district = districtInfoService.getById(apartmentSubmitVo.getDistrictId());
        apartmentSubmitVo.setCityName(city.getName());
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());
        apartmentSubmitVo.setDistrictName(district.getName());

        super.saveOrUpdate(apartmentSubmitVo);
        Long apartmentId = apartmentSubmitVo.getId();



        if (apartmentSubmitVo.getId()!=null) {
            //删除配套关系
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());

            apartmentFacilityMapper.delete(apartmentFacilityLambdaQueryWrapper);
            //删除标签关系
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelMapper.delete(apartmentLabelLambdaQueryWrapper);

            //删除杂费
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            apartmentFeeValueMapper.delete(apartmentFeeValueLambdaQueryWrapper);
            //删除图片
            LambdaQueryWrapper<GraphInfo> graphLambdaQuerWrapper = new LambdaQueryWrapper<>();
            graphLambdaQuerWrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphLambdaQuerWrapper.eq(GraphInfo::getItemType, APARTMENT);

            graphInfoMapper.delete(graphLambdaQuerWrapper);
        }
        //新增配套
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();


        List<ApartmentFacility> apartmentFacilities = new ArrayList<>();
        for (Long facilityInfoId : facilityInfoIds) {
            apartmentFacilities.add(
                    ApartmentFacility.builder().
                    facilityId(facilityInfoId)
                    .apartmentId(apartmentSubmitVo.getId())
                    .build())
            ;

        }

        apartmentFacilityService.saveOrUpdateBatch(apartmentFacilities);
        //新增标签
        List<ApartmentLabel> apartmentLabelarrayList = new ArrayList<ApartmentLabel>();
        for (Long labelId : labelIds) {
            apartmentLabelarrayList.add(
                    ApartmentLabel.builder().
                    labelId(labelId)
                    .apartmentId(apartmentSubmitVo.getId())
                    .build());
        }
        apartmentLabelService.saveBatch(apartmentLabelarrayList);


    //新增杂费
        List<Long>feeId=apartmentSubmitVo.getFeeValueIds();

        List<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
        for (Long feeValueId : feeId) {
            apartmentFeeValues.add(
                    ApartmentFeeValue.builder()
                            .feeValueId(feeValueId).build());


        }
        apartmentFeeValueService.saveBatch(apartmentFeeValues);

        // 新增图片
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if(null!=graphVoList&&graphVoList.size()>0){
            List<GraphInfo> graphInfos = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentId);
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }

    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);


        // TODO: 2023/10/18  实现公寓详情查询
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(graphInfoLambdaQueryWrapper);
        List<GraphVo> graphVoList = new ArrayList<>();
        for (GraphInfo graphInfo : graphInfos) {
            GraphVo graphVo = new GraphVo();
            graphVo.setName(graphInfo.getName());
            graphVo.setUrl(graphInfo.getUrl());
            graphVoList.add(graphVo);
        }

       List<FeeValueVo>feeList= feeValueMapper.selectListById(id);
        List<FacilityInfo>facilityInfoList=facilityInfoMapper.selectListById(id);
        List<LabelInfo>labelInfoList=labelInfoMapper.selectListById(id);
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setFeeValueVoList(feeList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);


        return apartmentDetailVo;
    }

    @Override
    public void updateReleaseStatusById(Long id, ReleaseStatus status) {
        LambdaUpdateWrapper<ApartmentInfo> apartmentInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        apartmentInfoLambdaUpdateWrapper
                .eq(ApartmentInfo::getId, id)
                .in(ApartmentInfo::getIsRelease, ReleaseStatus.RELEASED, ReleaseStatus.NOT_RELEASED)
                .set(ApartmentInfo::getIsRelease, status);



        apartmentInfoMapper.update(null, apartmentInfoLambdaUpdateWrapper);


    }


}